import applyUnitSelect from "@/components/comm-select/apply-unit-select";
import { doubleValidate } from "~/utils/validate";
import {
	fetchList,
	delObj,
	addObj,
	putObj,
	getObj,
	selectAll,
	saveSend,
	addSaveSend,
	getAllAssessCustomers
} from "@/views/assess/api/assess-task.js";
import { mapState } from "vuex";
export default {
	name: "assessTask",
	components: {
		applyUnitSelect: applyUnitSelect
	},
	provide () {
		return {
			initList: this.getListData
		};
	},
	data() {
		return {
			tableData: [], //所有模板
			// 请求参数
			params: {
				current: 1,
				size: 20,
				assessName: ""
			},
			// 分页
			pager: {
				total: 0, // 总页数
				current: 1, // 当前页数
				size: 20 // 每页显示多少条
			},
			filterList: [
				{
					label: "考核类型",
					inputType: "dict",
					dictName: "assess_cata",
					name: "assessCata",
					multiple: true
				}
			],
			formData: {}, //当前编辑的模板
			rules: {
				assessName: [
					{ required: true, message: "请输入考核名称", trigger: "blur" }
				],
				assessCata: [
					{ required: true, message: "请选择类别", trigger: "blur" }
				],
				assessDate: [
					{ required: true, message: "请选择考核日期", trigger: "blur" }
				],
				scoreLevel1: [
					{ required: true, message: "请输入及格分数", trigger: "blur" },
					{ validator: doubleValidate, trigger: "change" }
				],
				assessTemplateId: [
					{ required: true, message: "请选择考核模板", trigger: "blur" }
				]
			},
			dialogVisible: false, //新增弹出框
			value: [], //考核时间段
			customerNames: "", //考核单位名称
			assessTemplate: [], //考核模板
			dialogVisible2: false, //查看弹出框
			formData2: {}, //查看表单
			assessTemplateName: "", //查看表单的考核模板
			isEdit: "" ,//是否是编辑
			// 页面loading
			loading: false,
		};
	},
	computed: mapState({
		//考核类型
		assess_cata: state => {
			let data = state.dict.find(item => item.type === "assess_cata") || {};
			return data.children || [];
		}
	}),
	mounted() {
		selectAll().then(response => {
			if (response.data.code == 0) {
				var result = response.data.data;
				for (var i = 0; i < result.length; i++) {
					this.assessTemplate.push({
						label: result[i].name,
						value: result[i].assessTemplateId
					});
				}
			}
		});
		this.getListData();
	},
	watch: {
		customerNames: function() {
			this.formData.customerNames = this.customerNames;
		}
	},
	methods: {
		view(id) {
			this.dialogVisible2 = true;
			getObj(id).then(response => {
				if (response.data.code == 0) {
					this.formData2 = response.data.data;
					if (this.assessTemplate.length != 0) {
						for (var i = 0; i < this.assessTemplate.length; i++) {
							if (
								this.formData2.assessTemplateId == this.assessTemplate[i].value
							) {
								this.assessTemplateName = this.assessTemplate[i].label;
							}
						}
					}
				}
			});
		},
		//保存并发送
		saveAndSend() {
			if (
				this.formData.customerNames == "" ||
				this.formData.customerNames == null
			) {
				this.$confirm("未选择考核单位时，默认选择全部考核单位", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(() => {
					getAllAssessCustomers().then(response => {
						if (response.data.code == 0) {
							this.put(response.data.data, this.bbb);
						}
					});
				});
			} else {
				this.bbb();
			}
		},
		bbb() {
			this.$refs["formData"].validate(valid1 => {
				if (valid1) {
					this.formData.startTime = this.getDate(this.value[0]);
					this.formData.endTime = this.getDate(this.value[1]);
					this.formData.status = "01";
					if (this.isEdit) {
						saveSend(Object.assign(this.formData)).then(response => {
							if (response.data.code == 0) {
								this.$alert("保存成功");
								this.dialogVisible = false;
								this.getListData();
							} else {
								this.$alert("保存失败");
							}
						});
					} else {
						addSaveSend(Object.assign(this.formData)).then(response => {
							if (response.data.code == 0) {
								this.$alert("保存成功");
								this.dialogVisible = false;
								this.getListData();
							} else {
								this.$alert("保存失败");
							}
						});
					}
				}
			});
		},
		//将Date类型转换为string类型
		getDate(strDate) {
			var date = new Date(strDate);
			var returnValue = date.getFullYear();
			if (date.getMonth() + 1 < 10) {
				returnValue += "-0" + (date.getMonth() + 1);
			} else {
				returnValue += "-" + (date.getMonth() + 1);
			}
			if (date.getDate() < 10) {
				returnValue += "-0" + date.getDate();
			} else {
				returnValue += "-" + date.getDate();
			}
			return returnValue;
		},
		//发送
		save() {
			if (
				this.formData.customerNames == "" ||
				this.formData.customerNames == null
			) {
				this.$confirm("未选择考核单位时，默认选择全部考核单位", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(() => {
					getAllAssessCustomers().then(response => {
						if (response.data.code == 0) {
							this.dialogVisible = false;
							this.put(response.data.data, this.aaa);
						}
					});
				});
			} else {
				this.aaa();
			}
		},
		aaa() {
			this.$refs["formData"].validate(valid1 => {
				if (valid1) {
					this.formData.startTime = this.getDate(this.value[0]);
					this.formData.endTime = this.getDate(this.value[1]);
					this.formData.status = "00";
					if (this.isEdit) {
						putObj(Object.assign(this.formData)).then(response => {
							if (response.data.code == 0) {
								this.suc("修改成功");
								this.dialogVisible = false;
								this.getListData();
							} else {
								this.err("修改失败");
							}
						});
					} else {
						addObj(Object.assign(this.formData)).then(response => {
							if (response.data.code == 0) {
								this.suc("添加成功");
								this.dialogVisible = false;
								this.getListData();
							} else {
								this.err("添加失败");
							}
						});
					}
				}
			});
		},
		cancel() {
			this.dialogVisible = false;
		},
		handleClose(done) {
			this.$confirm("确认关闭？")
				.then(_ => {
					done();
				})
				.catch(_ => {});
		},
		async edit(id) {
			this.isEdit = true;
			this.dialogVisible = true;
			this.customerNames = null;
			const response = await getObj(id);
			let formData = response.data.data;
			this.formData = formData;
			this.value = [formData.startTime, formData.endTime];
			//this.value[0]=formData.startTime;//new Date(Date.parse(formData.startTime.replace(/-/g,   "/"))); //转换成Data();
			//this.value[1]=formData.endTime;//new Date(Date.parse(formData.endTime.replace(/-/g,   "/")));
			this.customerNames = formData.customerNames;
		},
		add() {
			this.isEdit = false;
			this.dialogVisible = true;
			this.customerNames = null;
			this.formData = {};
			this.value = [];
		},
		closeAssess(row) {
			this.confirm("结束考核确认", "您确定要结束考核吗？", () => {
				row.status = "02";
				putObj(Object.assign(row)).then(response => {
					if (response.data.code == 0) {
						this.suc("已经结束考核");
						this.getListData();
					} else {
						this.err("结束考核失败");
					}
				});
			});
		},
		del(id) {
			this.confirm("删除确认", "您确定要删除该数据吗？", () => {
				delObj(id).then(response => {
					if (response.data.code == 0) {
						this.suc("删除成功");
						this.getListData();
					} else {
						this.err("删除失败");
					}
				});
			});
		},
		//选择考核单位
		selectApplyUnit() {
			this.$refs.applyUnitSelect.show();
		},
		completeSelect4(selectedData) {
			this.put(selectedData);
		},
		//将考核单位列表放到表单中
		put(selectedData, callback) {
			var ids = "";
			var names = "";
			if (selectedData.length != 0) {
				for (var i = 0, len = selectedData.length; i < len - 1; i++) {
					ids += selectedData[i].apUiId + ",";
					names += selectedData[i].unitName + ",";
				}
				ids += selectedData[selectedData.length - 1].apUiId;
				names += selectedData[selectedData.length - 1].unitName;
			}
			this.formData.customerIds = ids;
			this.customerNames = names;
			callback && callback();
		},
		getListData (params = {}) {
			this.loading = true;
			let requestData = { ...this.params, ...params };
			fetchList(requestData)
				.then(res => {
					const { current, size, total, records } = res.data.data;
					this.tableData = records;
					this.params = requestData;
					this.pager = { total, current, size };
				})
				.finally(() => {
					this.loading = false;
				});
		},
		// 菜单点击监听
		handleMenuClick (type) {
			switch (type) {
				case "add":
					this.add();
					break;
				default:
					this.$message(type);
					break;
			}
		}
	}
};
